package com.bioroadlab.clinicial;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.mysql.jdbc.Statement;

public class ClinicalSynopses {
	private static File fileExcel = new File("C:\\Users\\Administrator\\Desktop\\小芳姐的excel\\clicnical.xls");
	
	public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException{
		if(!fileExcel.exists()){
			HSSFWorkbook workBook1 = new HSSFWorkbook();
			HSSFSheet sheet1 = workBook1.createSheet("clicial");
			HSSFRow titleRow1 = sheet1.createRow(0);
			HSSFCell[] cellArray = new HSSFCell[3];
			for (int i = 0; i < cellArray.length; i++) {
				cellArray[i] = titleRow1.createCell(i);
			}
			cellArray[0].setCellValue("表型OMIM号");
			cellArray[1].setCellValue("疾病名称");
			cellArray[2].setCellValue("其他");
			
			FileOutputStream out = new FileOutputStream(fileExcel, true);
			workBook1.write(out);
			out.flush();
			out.close();
		}
		
			long startTime=new Date().getTime();
			File file =new File("C:\\Users\\Administrator\\Desktop\\小芳姐的excel\\diseaseOMIMNum.txt");
			String str="";
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn=DriverManager.getConnection("jdbc:mysql:///diseaseoflinkcapture111-3","root","root");
			Statement stat=(Statement) conn.createStatement();
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
				while((str = reader.readLine()) != null) {
					str = str.trim();
					//去页面抓取，再存数据库
					
					ResultSet rs = stat.executeQuery("select diseaseNum from t_diseasenum");
					List<String> listNum = new ArrayList<String>();
					while(rs.next()){
						String rsOfdiseaseNum = rs.getString("diseaseNum");
						listNum.add(rsOfdiseaseNum);
					}
					if(!listNum.contains(str)){
						long start = new Date().getTime();
						captureWithDiseaseNum(str);
						System.out.println(str+"\t用时:" + (new Date().getTime()-start) + "毫秒" );
						str="insert into t_diseasenum(diseaseNum) value('"+str+"');";
						stat.execute(str);
					}else if(listNum.contains(str)){
						System.out.println("已经存在，没有存库，没有存excel");
					}
					
				}
				reader.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}  finally{
				stat.close();
				conn.close();
			}
			System.out.println((new Date().getTime()-startTime)+"毫秒");
	}

	private static void captureWithDiseaseNum(String str) {
		String string ="http://www.omim.org/clinicalSynopsis/"+ str +"?search="+ str +"&highlight="+str;
		
		StringBuffer mimNum = new StringBuffer("");
		StringBuffer diseaseName = new StringBuffer("");
		StringBuffer strBuff = new StringBuffer("");
		try {
			URL url = new URL(string);
			
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			InputStreamReader input = new InputStreamReader(conn.getInputStream(), "UTF-8");
			BufferedReader buffReaderRead = new BufferedReader(input);
			String str1 = "";
			while ((str1 = buffReaderRead.readLine()) != null) {
				if(str1.contains("OMIM Clinical Synopsis")){
					buffReaderRead.readLine();
					str1 = buffReaderRead.readLine();
					if(!StringUtil.getSupString(str1).trim().replaceAll(" ", "").equals("")){
						String mNum = str1.substring(str1.indexOf(" - ")+2, str1.lastIndexOf(" - "));
						mimNum.append(mNum.trim());
						String name =str1.substring(str1.lastIndexOf(" - ")+2, str1.length());
						diseaseName.append(name.trim()); 
					}
				}
				
				if(!str1.contains(">-<")){
					if(str1.contains("<tr><td class=\"clinical-synopsis-category-value text-font lookup value-border")){
						str1 = StringUtil.getSupString(str1);
						String s = buffReaderRead.readLine();
						for (int i = 0; i < 100; i++) {
							s = s + buffReaderRead.readLine();
							if(s.contains("colspan=\"3\"")){
								break;
							}
						}
						String[] strSub1 = s.split("<td class=\"clinical-synopsis-subcategory-value text-font lookup value-border\">");
						for (String string2 : strSub1) {
							if(!StringUtil.getSupString(string2).replaceAll(" ", "").equals("")){
								String[] strSub2 = string2.split("<td class=\"clinical-synopsis-feature-value text-font lookup value-border-bottom\">");
								String dd = strSub2[0]+":"+strSub2[1];
								//System.out.println("\t--"+StringUtil.getSupString(dd));
								strBuff.append(str1.trim() + "→" + StringUtil.getSupString(dd).trim()+";");
							}
						}
					}
				}else if(str1.contains(">-<")){
					//System.out.println(str1);
					if(str1.contains("<td class=\"clinical-synopsis-subcategory-value text-font value-border\">")){
						String first = str1.substring(0, str1.indexOf("<td class=\"clinical-synopsis-subcategory-value text-font value-border\">"));
						
						str1 = str1.substring(str1.indexOf("<td class=\"clinical-synopsis-feature-value text-font lookup value-border-bottom\">")+"<td class=\"clinical-synopsis-feature-value text-font lookup value-border-bottom\">".length(),str1.length());
						strBuff.append(first+"→-:"+str1);
						//System.out.println("s的值"+StringUtil.getSupString(strBuff.toString()));
					}else if(str1.contains("<td class=\"clinical-synopsis-category-value text-font lookup value-border\">")){
						
						//System.out.println("???"+str1);
						if(str1.contains("<td class=\"clinical-synopsis-subcategory-value text-fon lookupt value-border\">")){
							String first = str1.substring(0, str1.indexOf("<td class=\"clinical-synopsis-subcategory-value text-fon lookupt value-border\">"));
							str1 = str1.substring(str1.indexOf(">-<")+2,str1.length());
							strBuff.append(first.trim()+"→-:"+str1.trim());
							//System.out.println("s的值"+StringUtil.getSupString(strBuff.toString()));
						}else if(str1.contains("<td class=\"clinical-synopsis-feature-value text-font lookup value-border-bottom\">")){
							String first = str1.substring(0, str1.indexOf("<td class=\"clinical-synopsis-feature-value text-font lookup value-border-bottom\">"));
							str1 = str1.substring(str1.indexOf(">-<")+2,str1.length());
							strBuff.append(first.trim()+"→-:"+str1.trim());
							//System.out.println("s的值"+StringUtil.getSupString(strBuff.toString()));
						}
					}
				}
			}
			String otherStr = StringUtil.getSupString(strBuff.toString());
			/*System.out.println("号："+mimNum);
			System.out.println("名："+diseaseName);
			System.out.println("总的："+otherStr);*/
			//str病号，导出excel存库
			//exportExcel();
			if(otherStr.length()>32767){
				BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\小芳姐的excel\\字符超出的.txt")));
				w.write(mimNum+"→"+otherStr+"\n");
				w.flush();
				w.close();
				otherStr = "超出啦在文本中在文件中：C:\\Users\\Administrator\\Desktop\\小芳姐的excel\\字符超出的.txt";
			}
			if("".equals(mimNum.toString().trim())){
				mimNum = new StringBuffer("#"+str);
			}
			
			HSSFWorkbook workBook = new HSSFWorkbook(new FileInputStream(fileExcel));
			HSSFSheet sheet = workBook.getSheet("clicial");
			HSSFRow titleRow = sheet.getRow(sheet.getLastRowNum());
			titleRow = sheet.createRow(sheet.getLastRowNum() + 1);
			titleRow.createCell(0).setCellValue(mimNum.toString());
			titleRow.createCell(1).setCellValue(diseaseName.toString());
			titleRow.createCell(2).setCellValue(otherStr.trim());
			
			FileOutputStream out = new FileOutputStream(fileExcel);
			out.flush();
			workBook.write(out);
			out.close();
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
}
